package com.zsy.controller;

import com.zsy.annoation.ReportLogAnno;
import com.zsy.pojo.Clazz;
import com.zsy.pojo.PageResult;
import com.zsy.pojo.Result;
import com.zsy.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-07-30 18:12
 **/
@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
//    ClazzService clazzService = new ClazzServiceImpl();

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @param begin
     * @param end
     * @return
     */
    @GetMapping
    public Result getClazz(
            Integer page,
            Integer pageSize,
            String name,
            @DateTimeFormat(pattern = "yyyy-dd-MM") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-dd-MM") LocalDate end
    ){
        log.info("查询所有班级信息");
        PageResult<Clazz> pageResult = clazzService.getClazz(page,pageSize,name,begin,end);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getClazzById(@PathVariable Integer id){
        Clazz clazz = clazzService.getClazzById(id);
        return Result.success(clazz);
    }

    /**
     * 查询全部班级
     * @return
     */
    @GetMapping("/list")
    public Result getClazzList(){
        List<Clazz> clazzs = clazzService.getClazzList();
        log.info(String.valueOf(clazzs));
        return Result.success(clazzs);
    }
    /**
     * 添加班级信息
     * @param clazz
     * @return
     */
    @PostMapping
    @ReportLogAnno
    public Result addClazz(@RequestBody Clazz clazz){
        System.out.println("添加班级信息："+clazz);
        clazzService.addClazz(clazz);
        return Result.success(clazz);
    }

    /**
     * 根据id删除班级
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ReportLogAnno
    public Result delClazzById(@PathVariable Integer id){
        Integer count = clazzService.delClazzById(id);
        if (count == 0) {
            return Result.success();
        } else
            return Result.error("对不起，该班级下面有学生,不能直接删除");
    }

    /**
     * 修改班级信息
     * @param clazz
     * @return
     */
    @PutMapping
    @ReportLogAnno
    public Result updateClazzById(@RequestBody Clazz clazz){
        log.info("修改的班级:" + clazz);
        clazzService.updateClazzById(clazz);
        return Result.success();
    }

}
